<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$couponset=$this->plugin_setting('coupon');
	$coupontype=intval($_RQ['coupontype']);
	if($coupontype !=10){
		$params = array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($coupontype){
			$params['coupontype']=$coupontype;
		}
		$getcoupon=pdo_getall('jy_weishop_coupon',$params);
		foreach($getcoupon as $k=>&$item){
			$limittimes2=json_decode($item['limittimes'],true);
			$item=$this->model('coupon')->intcoupon($item['id']);
			if($item['issendtime']){
				$item['sendtime']=$item['sendtime']?json_decode($item['sendtime'],true):array();
				$item['etime']=$item['sendtime']['tend'];
				if($item['sendtime']['tstart']>time()){
					$item['issend']=0;
					$item['etime']=$item['sendtime']['tstart'];
				}elseif($item['sendtime']['tstart']<time() && $item['sendtime']['tend']>time()){
					$item['issend']=1;
					$item['etime']=$item['sendtime']['tend'];
				}elseif($item['sendtime']['tend']<time()){
					$item['issend']=2;
				}
				$item['sendtime']['tend2']=$item['sendtime']['tend'];
				$item['sendtime']['tstart2']=$item['sendtime']['tstart'];
				$item['sendtime']['tend']=date('Y-m-d',$item['sendtime']['tend']);
				$item['sendtime']['tstart']=date('Y-m-d',$item['sendtime']['tstart']);
			}else{
				$item['issend']=1;
			}
			if($item['timelimittype']==2 && $limittimes2['tend']<time()){
				unset($getcoupon[$k]);
			}else{
				$coupon[]=$item;
			}	
		}
		
	}else{
		$params = array('uniacid'=>$_SESSION['uniacid'],'gettype'=>1);
		$coupon=pdo_getall('jy_weishop_wxcard',$params);
		foreach($coupon as &$row){
			if($row['card_type'] =='CASH'){
				$row['color'] = 'pink ';
				$tagtitle = '代金券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '立减' . (double) $row['reduce_cost'].'元';
			}else{
				$row['color'] = 'pink ';
				$tagtitle = '折扣券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '打' . (double) $row['discount'].'折';
			}
			if ($row['datetype'] == '0') {
					$title4 = '即' . $row['fixed_term'] . '天有效';
			}else{
					$row['timestamp']=json_decode($row['timestamp'],true);
					$title4 = $row['timestamp']['tstart'].'~'.$row['timestamp']['tend'];
			}			
			$row['title2'] = $title2;
			$row['title3'] = $title3;
			$row['title4'] = $title4;
			$row['title5'] = $title5;
		}
		//用户领取卡券
		$res=cfc('account')->request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type=wx_card');
		$api_ticket = $res['ticket'];
		$params=array(
			'api_ticket'=>$api_ticket,
			'timestamp'=>time(),
			'card_id'=>$row['card_id'],
		);
		$signature = sort($params);
		$cardExt = array('openid'=>$_SESSION['openid'],'timestamp'=>time(),'outer_str'=>'jy_weishop','signature'=>$signature);
		$row['cardExt'] = json_decode($cardExt,true);
	}
	$return['coupontype']=$coupontype;
	$return['couponset']=$couponset;
	$return['list']=$coupon;
	$return['ntime']=time();
	set_html('title',$setting['title']?'优惠券-'.$setting['title']:'优惠券-领取中心');
	exi($return);
}
if($_RQ['op']=='tx'){
	$post=get_postdata();
	$id = intval($post['id']);
	pdo_insert('jy_weishop_sendmsg',array('mid'=>$_SESSION['uid'],'couponid' => $id,'time'=>time(),'issend'=>0,'msg'=>'优惠券领券提醒！'));
	exi('设置成功');
}
?>